import logging
import unittest

import requests
from parameterized import parameterized

from api.login_api import LoginApi
from api.tender_api import TenderAPI
from utils import common_assert, read_html, read_data


class TestTenderApi(unittest.TestCase):
    def setUp(self):
        self.session = requests.Session()
        logging.info(F"创建session对象地址{self.session}")
        self.client = TenderAPI(self.session)
        LoginApi(self.session).login_api()


    def tearDown(self):
        self.session.close()
        logging.info(F"session对象{self.session}被关闭")

    @parameterized.expand(read_data("tender_data.json", "tender_api"))
    def test01_tender_api(self, loan_id, amount, password, expect):
        try:
            resp = self.client.tender_api(loan_id, amount, password)
            logging.info(f"预取结果: {expect} / 响应结果: {resp.text}")
            common_assert(self, resp, expect)
            logging.info("断言通过")
            if resp.json()["status"] == 200:
                result = read_html(resp.json()["description"]["form"], "html.parser", "input")
                resp = self.client.tripartite_tender_api(result[0], result[1])
                logging.info(f"响应结果: {resp.text}")
                self.assertIn("OK", resp.text)
                logging.info("断言通过")
        except Exception as e:
            logging.error(f"断言失败: {e}")
            raise